var userList = new Ext.data.JsonStore({
    url: 'user_usersOfRole_list.action',
    root: 'humans',
    fields: ['userId', 'userName']
});

userList.load();
var inputWords=0;

var ds = new Ext.data.Store({
    proxy: new Ext.data.HttpProxy({
        url: 'statistics_inputWords_list.action',
        method: 'POST'
    }),
    reader: new Ext.data.JsonReader({
        root: 'persons'
    }, [{
        name: 'statType',
        mapping: 'statType',
        convert: function(value, record){
            if (value == 49) {
                return '录入翻译字数';
            }else if (value == 50) {
                return '录入校审字数';
            }else if (value == 51) {
                return '录入校对字数';
            }else if (value == 52) {
                return '录入审核字数';
            }
        },
        type: 'short'
    
    }, {
        name: 'num',
        mapping: 'num',
		convert: function(value,record){
			if(value!=0){
				inputWords =inputWords+value;
			}
			return value;
		},
        type: 'long'
    }])
});

ds.on('load',ofLoad);

function ofLoad(){
	Ext.getCmp('inputWords').setValue(inputWords);
	inputWords=0;
}
var cm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
    header: '统计类型',
    width: 200,
    dataIndex: 'statType',
    sortable: true
}, {
    header: "录入字数",
    width: 200,
    dataIndex: 'num',
    sortable: true
}]);

var text=['录入字数'];
var tbarName=[];
var statType=0;
var statisticsCls=['statistics_text','statistics_manuscript','statistics_hday'];
for(var i=0;i<text.length;i++){
	tbarName[i]= new Ext.Button({
		text: text[i],
		iconCls: statisticsCls[i],
		width: 100,
		handler: function(value){
		if(value.text=='录入字数'){
			statType=1;
		}
		var currentYear = new Date().getFullYear();
		
		var comboYearData = [['1', eval(currentYear)], ['2', eval(currentYear) - 1], ['3', eval(currentYear) - 2], ['4', eval(currentYear) - 3], ['5', eval(currentYear) - 4], ['6', eval(currentYear) - 5], ['7', eval(currentYear) - 6], ['8', eval(currentYear) - 7], ['9', eval(currentYear) - 8], ['10', eval(currentYear) - 9], ['11', eval(currentYear) - 10]];
		
		var comboQuarterData = [['4', '4'], ['3', '3'], ['2', '2'], ['1', '1']];
		
		var comboMonthData = [['12', '12'], ['11', '11'], ['10', '10'], ['9', '9'], ['8', '8'], ['7', '7'], ['6', '6'], ['5', '5'], ['4', '4'], ['3', '3'], ['2', '2'], ['1', '1']];
		
		var dstore=new Ext.data.SimpleStore({
			data: comboYearData,
			fields: ['value', 'text']
		});
		var yearOne = new Ext.form.ComboBox({
			store:dstore,
			name: 'year',
			fieldLabel: '年',
			emptyText: '请选择',
			mode: 'local',
			triggerAction: 'all',
			valueField: 'value',
			displayField: 'text',
			itemCls: 'form_text',
			id: 'yearOne',
			editable: false,
			columnWidth: .33
		});
		
		var quarterOne = new Ext.form.ComboBox({
			store: new Ext.data.SimpleStore({
				fields: ['value', 'text'],
				data: comboQuarterData
			}),
			name: 'quarter',
			fieldLabel: '季',
			emptyText: '请选择',
			mode: 'local',
			triggerAction: 'all',
			valueField: 'value',
			displayField: 'text',
			itemCls: 'form_text',
			id: 'quarterOne',
			editable: false,
			columnWidth: .3
		});
		
		var monthOne = new Ext.form.ComboBox({
			store: new Ext.data.SimpleStore({
				fields: ['value', 'text'],
				data: comboMonthData
			}),
			name: 'month',
			fieldLabel: '月',
			emptyText: '请选择',
			mode: 'local',
			triggerAction: 'all',
			valueField: 'value',
			displayField: 'text',
			itemCls: 'form_text',
			id: 'monthOne',
			editable: false,
			columnWidth: .3
		});
		
		var tab_time = new Ext.Panel({
			title: '选择区间',
			layout: 'column',
			id: 'tab_timeZone',
			plain: true,
			frame: true,
			cls: "clear_l",
			hidden: 'true',
			items:[yearOne,quarterOne,monthOne]
		});
		
		var statisticsInputForm = new Ext.form.FormPanel({
			labelAlign: 'right',
			labelWidth: 80,
			frame: true,
			width: 400,
			items: [{
				xtype: 'combo',
				store: userList,
				id:'inputPersonName',
				allowBlank: false,
				editable: false,
				fieldLabel: '人员姓名',
				emptyText: '请选择',
				mode: 'local',
				triggerAction: 'all',
				valueField: 'userId',
				displayField: 'userName',
				itemCls: 'form_text'
			
			}, {
				autoHeight: true,
				hideLabels: true,
				defaultType: 'radio',
				width: 200,
				items: [{
					boxLabel: '按年统计',
					name: 'periodType',
					inputValue: 1,
					id: 'workloadRadioOne',
					listeners: {
					'check': function(obj, checked){
					if (checked) {
						tab_time.show();
						yearOne.show();
						quarterOne.hide();
						monthOne.hide();
					}
				}
				}
				}, {
					boxLabel: '按季统计',
					name: 'periodType',
					inputValue: 2,
					id: 'workloadRadioTwo',
					listeners: {
					'check': function(obj, checked){
					if (checked) {
						tab_time.show();
						yearOne.show();
						quarterOne.show();
						monthOne.hide();
					}
				}
				}
				}, {
					boxLabel: '按月统计',
					name: 'periodType',
					inputValue: 3,
					id: 'workloadRadioThree',
					listeners: {
						'check': function(obj, checked){
							if (checked) {
								tab_time.show();
								yearOne.show();
								quarterOne.hide();
								monthOne.show();
							}
					}
				}
				}]
			}, tab_time],
			buttons: [{
				text: '统计',
				type: 'submit',
				handler: function(){
				
				var flag = true;
				if (!Ext.getCmp('workloadRadioOne').checked &&
						!Ext.getCmp('workloadRadioTwo').checked &&
						!Ext.getCmp('workloadRadioThree').checked) {
					flag = false;
				}
				else {
					if (Ext.getCmp('workloadRadioOne').checked) {
						if (Ext.getCmp('yearOne').getValue() ==
						'') {
							flag = false;
						}
					}
					else 
						if (Ext.getCmp('workloadRadioTwo').checked) {
							if (Ext.getCmp('yearOne').getValue() ==
								'' ||
								Ext.getCmp('quarterOne').getValue() ==
							'') {
								flag = false;
							}
						}
						else 
							if (Ext.getCmp('workloadRadioThree').checked) {
								if (Ext.getCmp('yearOne').getValue() ==
									'' ||
									Ext.getCmp('monthOne').getValue() ==
								'') {
									flag = false;
								}
							}
				}
				if (flag == false) {
					Ext.Msg.show({
						title: '错误提示',
						msg: '请选择时间区间类型和对应的区间！',
						buttons: Ext.Msg.OK,
						icon: Ext.Msg.INFO,
						width: 200
					});
					return;
				}
				
				if (statisticsInputForm.form.isValid()) {
					var inputPersonId=Ext.getCmp('inputPersonName').value;//录入人的ID
					statisticsInputForm.form.doAction('submit', {
						url: 'statistics_inputWords_list.action',// 文件路径
						method: 'post',// 提交方法post或get
						params: {
						inputPersonId:inputPersonId,
						statType: statType,
						subjectType: 3
					},
					success: function(form, action){
						
						var message = "统计区间内有未确认的";
						
						if(value.text=='录入字数')
						{
							if ((action.result.inputWordNumFlag & 0x0001) == 0x0001) {
								message = message + '翻译记录  ';
							}
							if ((action.result.inputWordNumFlag & 0x0010) == 0x0010) {
								message = message + '校审记录';
							}
							if ((action.result.inputWordNumFlag & 0x0100) == 0x0100) {
								message = message + '校对记录  ';
							}
							if ((action.result.inputWordNumFlag & 0x1000) == 0x1000) {
								message = message + '审核记录  ';
							}
						}
						
						/*else if(value.text=='稿件数量')
						{
							if ((action.result.manuscriptNumFlag & 0x0001) == 0x0001) {
                                    message = message + '翻译记录  ';
                                }
                                if ((action.result.manuscriptNumFlag & 0x0010) == 0x0010) {
                                    message = message + '校审记录  ';
                                }
                                if ((action.result.manuscriptNumFlag & 0x0100) == 0x0100) {
                                    message = message + '校对记录  ';
                                }
                                if ((action.result.manuscriptNumFlag & 0x1000) == 0x1000) {
                                    message = message + '审核记录  ';
                                }
						}
						*/
						
						if (message != "统计区间内有未确认的") {
							Ext.Msg.show({
								title: '错误提示',
								msg: message + "!",
								buttons: Ext.Msg.OK,
								icon: Ext.Msg.INFO,
								width: 200
							});
						}
						
						personName = action.result.personName;
						Ext.getCmp('perN').setValue(personName);
						
						ds.baseParams = statisticsInputForm.getForm().getValues();
									ds.load({
										params : {
										    inputPersonId:inputPersonId,
											statType : statType,
											subjectType : 3
										}
									});
						
						window_statictics_workload.close();
					},
					failure: function(form, action){
						if (action.failureType == 'server') {
							Ext.Msg.show({
								title : '消息',
								msg : '操作失败',
								buttons : Ext.Msg.OK,
								icon : Ext.Msg.INFO,
								width : 200
							});
						} else 
							if (action.failureType == 'connect') {
								Ext.Msg.show({
									title : '连接错误',
									msg : '指定路径连接错误!',
									buttons : Ext.Msg.OK,
									icon : Ext.Msg.ERROR,
									width : 200
								});
							}
							else 
								if (action.failureType == 'client') {
									Ext.Msg.show({
										title : '提示',
										msg : '数据错误，非法提交',
										buttons : Ext.Msg.OK,
										icon : Ext.Msg.ERROR,
										width : 200
									});
								}
								else {
									Ext.Msg.show({
										title : '警告',
										msg : '服务器数据传输错误',
										buttons : Ext.Msg.OK,
										icon : Ext.Msg.ERROR,
										width : 200
									});
								}
					}
					});
				}
			}
			}, {
				text: '取消',
				handler: function(){
				window_statictics_workload.close();
			}
			}]
		});
		
		var window_statictics_workload = new Ext.Window({
			title: value.text+'统计',
			width: 400,
			iconCls: value.iconCls,
			resizable: false,
			autoHeight: true,
			modal: true,
			closeAction: 'hide',
			closable: false,
			items: statisticsInputForm
		}).show();
	}
	});
}

var personName = '';

var picture = new Ext.Button({
    text: '生成柱状图',
    iconCls: "statistics_bar",
    width: 100,
    handler: function(){
    
        var chartPanel = new Ext.Panel({
            width: 500,
            height: 300,
            layout: 'fit',
            frame: true,
            items: {
                xtype: 'columnchart',
                url: 'images/charts.swf',
                store: ds,
                yField: 'num',
                series: [{
                    xField: 'statType'
                }],
                listeners: {
                    itemclick: function(o){
                        var rec = ds.getAt(o.index);
                        Ext.example.msg('Item Selected', 'You chose {0}.', rec.get('name'));
                    }
                }
            }
        });
        
        var win_delete_work = new Ext.Window({
            title: '统计图',
            height: 330,
            width: 550,
            closable: true,
            closeAction: 'hide',
            modal: true,
            items: [chartPanel]
        }).show();
        
    }
});

var perName2 = new Ext.form.Label({
	text: '人员姓名：',
	cls:'input_label_text'
});

var perName = new Ext.form.TextField({
	cls:'input_label',
	width: 100,
    readOnly: true,
    id: 'perN'
});
var inputWordsLable=new Ext.form.Label({
	text:'录入总字数：',
	cls:'input_label_text'
});
var inputWordsTextField=new Ext.form.TextField({
	cls: 'input_label',
	width: 100,
	readOnly: true,
	id:'inputWords'
});
var grid = new Ext.grid.GridPanel({
    title: '当前位置：录入人员工作统计',
    loadMask: {
        msg: '数据加载中...'
    },
    ds: ds,
    store: ds,
    region: 'center',
    autoScroll: true,
    cm: cm,
//    autoExpandColumn: 1,
    tbar: [tbarName, picture,perName2,perName,inputWordsLable,inputWordsTextField]
});

var Input_statisticsInput = {
    id: 'statisticsInput-statistics',
    border: false,
    layout: 'border',
    items: [grid]
};
Ext.getCmp('content-panel').add(Input_statisticsInput);
Ext.getCmp('content-panel').layout.setActiveItem('statisticsInput-statistics');
Ext.getCmp('content-panel').uxmodules="undefined";